<template>
    <div class="carCont" :style="'min-height: '+docheight+'px'">
      <div class="goodsList" v-if="!showTimeOut&&showCont">
            <div class="mainHead">
                <div class="mainTitle">我的收藏</div>
                <div class="checkCont" @click="checkAllFn">
                    <div :class="isAll?'isCheck checkAll iconfont icon-gouxuan1':'checkAll iconfont icon-gouxuan1'"></div>
                    <div class="checkText">全选</div>
                </div>
            </div>
            <div class="goodsItem" :key="index2" v-for="(item,index2) in cartArr">
              <div @click="checkOneFn(item)" :class="item.check?'isCheck  checkOne iconfont icon-gouxuan1':'checkOne iconfont icon-gouxuan1'"></div>
              <div class="goodsImg" @click="toDetail(item)">
                <img :src="item.goods_image+ossSrc">
              </div>
              <div class="goodsInfo">
                <div class="goodsName" @click="toDetail(item)">{{ item.goods_name }}</div>
                <div class="goodsDes">{{ item.goods_describe }}</div>
                <div class="goodsCtrl">
                  <div class="goodsPrice">￥{{ item.goods_price }}</div>
                </div>
              </div>
            </div>
            <div class="loadMore" v-if="cartArr.length>0">{{ loadMoreText }}</div>
            <nodata v-if="cartArr.length==0" text="收藏记录空空如也~"></nodata>         
          </div>
        <div class="carCtrl">
            <div :class="carCheckArr.length==0?'buyBtn notBuy':'buyBtn'" @click="saveFn">取消收藏({{ carCheckArr.length }})</div>
        </div>
      <getLoad v-if="!showCont"></getLoad>
      <sub-load v-if="showLoad"></sub-load>
        <toast v-model="showToast" position="middle" width="50%" type="text" :time="1000">{{showText}}</toast>
    </div>
  </template>
  
  <script>
import nodata from '@/components/public/nodata'

import getLoad from '@/components/public/getLoad'
  import getTimeOut from '@/components/public/getTimeOut'
  import {Toast } from 'vux'
  import subLoad from '@/components/public/subLoad.vue'

  import { collectList,canceCollects } from '@/http/api'
  export default {
    components: {subLoad,nodata,Toast,getTimeOut,getLoad},
    data() {
      return {
        showTimeOut:false,
        showCont:false,
        showText:'',
        showToast:false,
        showLoad:false,
        cartArr:[],
        isAll:false,
        carCheckArr:[],
        checkNum:0,
        // 分页
        per_page:20,
        page:1,
        loadMoreText:'数据加载中~',
        getting:false,
        last_page:0,
      }
    },
    props: {},
    methods: {
      toDetail(item){
        this.$router.push({path:'/goodsDetail',query:{id:item.goods_id}})
      },
      getCheckArr(){
        this.carCheckArr = []
        for(let i = 0; i <this.cartArr.length; i++){
            if(this.cartArr[i].check){
                this.carCheckArr.push(this.cartArr[i])
            }
        }
      },
      saveFn(){
        if(this.carCheckArr.length>0){
            this.delFn()
        }else{
            this.showToast = true
            this.showText = '请选择取消的收藏商品'
        }
      },
      delFn(){
        let goods_ids = []
        for(let i = 0; i <this.carCheckArr.length; i++) {
            goods_ids.push(this.carCheckArr[i].goods_id)
        }
        let data= {
            goods_id:goods_ids.join(","),
        }
        console.log(data)
        canceCollects(data).then((res)=>{
            if(res.data.code==1){
                this.cartArr = []
                this.page = 1
                this.last_page  =0
                this.carCheckArr = []
                this.isAll = false
                this.getData()
            }else{
                this.showToast = true
                this.showText = res.data.msg
            }
        }).catch(()=>{
            this.showToast = true
            this.showText = '网络请求超时'
        })
      },
      checkAllFn(){
        this.isAll = !this.isAll
        for(let i=0;i<this.cartArr.length;i++){
            this.cartArr[i].check = this.isAll
        }
        this.getCheckArr()
      },
      checkOneFn(item){
        item.check = !item.check
        let result = true
        if(item.check){
            for(let i  = 0; i < this.cartArr.length;i++){
                if(!this.cartArr[i].check){
                    result = false
                }
            }
        }else{
            result = false
        }
        this.isAll = result
        this.getCheckArr()
      },
      getData(){
        let data = {
            page:this.page,
            per_page:this.per_page,
        }
        collectList(data).then((res)=>{
          if(res.data.code==1){
            let arr = res.data.data.data
            for(let i=0;i<arr.length;i++){
                arr[i]['check'] = false
                this.cartArr.push(arr[i])
            }
            setTimeout(() => {
                this.getting = false
            }, 100);
            this.last_page = res.data.data.last_page
            if(this.page==this.last_page){
                this.loadMoreText = '到底啦～'
            }else{
                this.loadMoreText = '数据加载中~'
            }

          }else{
            this.showToast = true
            this.showText = res.data.msg
          }
          this.showCont= true
        }).catch(()=>{
        this.showTimeOut = true
        this.showCont= true
      })
      },
      nextfn(){
      if(this.$route.path == '/saveGoods'){
        let h = document.documentElement.scrollHeight||document.body.scrollHeight
        let wh = document.documentElement.clientHeight||document.body.clientHeight
        let top = document.documentElement.scrollTop||document.body.scrollTop
        let bottom = h-wh-top
        if(bottom<=50){
          if(this.getting==false){
            if(this.last_page>this.page){
              this.getting=  true
              this.page++
              this.getData()
            }
          }
        }
      }
    }
    },
    mounted() {
    },
    created() {
      this.getData()
    },
  }
  </script>
  
  <style scoped lang="less">
  .carCont{
    width: 100%;
    background-color: #edf2ff;
    padding-bottom: 4.25rem;
    box-sizing: border-box;
    padding-top: 0.75rem;
    .goodsList{
        width: 92%;
        margin: 0 auto;
        border-radius: 0.75rem;
        background-color: #fff;
        padding: 0.75rem 4% 1.25rem;
        box-sizing: border-box;
        .mainHead{
            display: flex;
            justify-content: space-between;
            padding: 0.75rem 0;
            margin-bottom: 0.75rem;
            .checkCont{
                display: flex;
                align-items:  center;
                font-size: 0.7rem;
                // color: #666;
                .checkAll{
                width: 1rem;
                height: 1rem;
                border-radius: 50%;
                background-color: #ebebeb;
                text-align: center;
                color: white;
                line-height: 1rem;
                margin-right: 0.5rem;
                }
                .isCheck{
                    background: #6d94f8;
                }
                .checkText{
                    font-weight: bold;
                }
            }
            .mainTitle{
                font-weight: bold;
            }
        }
        .goodsItem{
        width: 100%;
        margin-bottom: 0.75rem;
        display: flex;
        align-items: center;
        .checkOne{
            width: 1rem;
            height: 1rem;
            border-radius: 50%;
            background-color: #ebebeb;
            text-align: center;
            line-height: 1rem;
            color: white;
        }
        .isCheck{
            background-color: #6d94f8;
        }
        .goodsImg{
            width: 4rem;
            height: 4rem;
            overflow: hidden;
            border-radius: 0.25rem;
            margin: 0 0.5rem;
            img{
            width: 100%;
            }
        }
        .goodsInfo{
            width: 0.5rem;
            flex: 1;
            height: 4rem;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .goodsName{
            width: 100%;
            margin-top: 0.15rem;
            font-size: 0.7rem;
            font-weight: bold;
            overflow: hidden; //超出的文本隐藏
            text-overflow: ellipsis; //溢出用省略号显示
            white-space: nowrap;
            }
            .goodsDes{
            width: 100%;
            font-size: 0.6rem;
            color: #999;
            overflow: hidden; //超出的文本隐藏
            text-overflow: ellipsis; //溢出用省略号显示
            white-space: nowrap;
            }
            .goodsCtrl{
            width: 100%;
            display: flex;
            height: 1.5rem;
            align-items: center;
            line-height: 1.5rem;
            margin-top: 0.25rem;
            justify-content: space-between;
            .goodsPrice{
                font-size: 0.7rem;
                color: #6d94f8;
            }
            .goodsNumCont{
                display: flex;
                height: 1rem;
                align-items: center;
                .editBtn{
                width: 1.7rem;
                height: 1.7rem;
                border-radius: 50%;
                background-color: #6d94f8;
                color: #fff;
                font-size: 0.7rem;
                text-align: center;
                line-height: 1.7rem;
                }
                .disBtn{
                    opacity: 0.4;
                }
                .goodsNum{
                width: 2rem;
                font-size: 0.7rem;
                text-align: center;
                }
            }
            }
        }
        }
        .loadMore{
          text-align: center;
          padding: 0.75rem 0;
          font-size: 0.6rem;
          color: #6d94f8;
        }
    }
    .likeCont{
        width: 92%;
        margin: 0 auto;
        margin-top: 1rem;
        background-color: #fff;
        padding: 0.75rem 4%;
        box-sizing: border-box;
        border-radius: 0.75rem;
        .mainHead{
            display: flex;
            justify-content: space-between;
            padding: 0.75rem 0;
            margin-bottom: 0.75rem;
            .mainTitle{
                font-weight: bold;
            }
            
        }
        .cosList{
        
        display: flex;
        flex-wrap: wrap;
        .cosItem,.maxCosItem{
          width: 30%;
          margin-left: 5%;
          margin-bottom: 0.75rem;
          .cosImg{
            width: 100%;
            position: relative;
            border-radius: 0.25rem;
            overflow: hidden;
            font-size: 0;
            .sortNum{
              position: absolute;
              padding: 0.25rem 0.5rem;
              // font-size: 0.6rem;
              left: 0;
              font-size: 0.6rem;
              top: 0;
              background-color: #6d94f8;
              color: white;
              border-radius: 0 0 0.25rem 0;
            }
            img{
              width: 100%;
            }
          }
          .cosItemInfo{
            .cosName{
                width: 100%;
                margin-top: 0.25rem;
                margin-bottom: 0.25rem;
                font-size: 0.7rem;
                font-weight: bold;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
              }
              .cosDes{
                color: #999;
                font-size: 0.6rem;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
              }
              .cosPrice{
                display: flex;
                align-items: center;
                color: #6d94f8;
                .svipTag{
                  // font-size: 0.6rem;
                  // background-color: #6d94f8;
                  // color: white;
                  // padding: 0.1rem 0.5rem;
                  // margin-right: 0.25rem;
                  // border-radius: 0.25rem;
                }
              }
          }
          
        }
        .maxCosItem{
         width: 48%;
         margin-left: 0;
        }
        .noLeft{
          margin-left: 0;
        }
      }
      .maxCosList{
        justify-content: space-between;
      }
    }
    
    .carCtrl{
      width: 100%;
    //   background-color: white;
      position:fixed;
      left: 0;
      align-items: center;
      padding: 0.75rem 4%;
      display: flex;
      bottom: -0.1rem;
      box-sizing: border-box;
      justify-content: flex-end;
      
      .buyBtn{
        width: 100%;
        padding: 0.6rem 1.5rem;
        background-color: #6d94f8;
        color: white;
        text-align: center;
        font-size: 0.6rem;
        border-radius: 1rem;
      }
      .notBuy{
        opacity: 0.5;
      }
    }
  }
  </style>
  